import React from 'react';
import { AutoComplete } from 'antd';
import projectServer from '../services/project';
import isSuccess from '../utils/error';

export default class Select extends React.Component {
    constructor(props) {
        super(props);
        this.state={
            dataSource: [],
            value: props.value
        }
    }


    onSelect = (value) => {
        this.setState({ value });
        this.props.onChange(value);
    }

    onChange = (value) => { // 用于清空的时候清除value的值
        if(!value) {
            this.setState({ value: '' });
            this.props.onChange(value);
        }
    }

    handleSearch = async (value) => {
        const res = await projectServer.getCourts({
            name: value,
            pageindex:1,
            pagesize:100
        });
        if(isSuccess(res)){
            // console.log('---AutoComplete getCourts---', res);
            const dataSource = res.data.map(v => ({ ...v, text: v.recname, value: v.recname}));
            this.setState({ dataSource });
        }  
    };
  
    render(){
        const { dataSource, value } = this.state;
        return (
            <AutoComplete
                allowClear
                defaultValue={value}
                dataSource={dataSource}
                onSelect={this.onSelect}
                onChange={this.onChange}
                onSearch={this.handleSearch}
                placeholder="请输入法院名称关键字"
            />
        )
    }  
}